package sword_to_1;

public class LeeCode_20 {
    public static void main(String[] args) {
//        System.out.println(isNumber("+100"));
//        System.out.println(isNumber("12e2.1 "));
//        System.out.println(isNumber("e9"));
//        System.out.println(isNumber(".1"));
//        System.out.println(isNumber("3."));
//        System.out.println(isNumber("0."));
//        System.out.println(isNumber("4e+"));
//        System.out.println(isNumber(".-4"));
        System.out.println(isNumber("+.8"));
        System.out.println(isNumber("7e69e"));
    }
    private static boolean isNumber(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'e' || s.charAt(i) == 'E'){
                count++;
            }
        }
        if (count > 1){
            return false;
        }
        int i = 0;
        while (i < s.length() && s.charAt(i) == ' '){
            i++;
        }
        int l = i;
        i = s.length() - 1;
        while (i >= 0 && s.charAt(i) == ' '){
            i--;
        }
        int r = i + 1;
        if (l > r){
            return false;
        }
        s = s.substring(l, r);
        String[] s1 = s.split("e");
        if (s1.length == 2){
            return (chargeInt(s1[0]) || chargeDouble(s1[0])) && chargeInt(s1[1]);
        }
        String[] s2 = s.split("E");
        if (s2.length == 2){
            return (chargeInt(s2[0]) || chargeDouble(s2[0])) && chargeInt(s2[1]);
        }
        return chargeDouble(s) || chargeInt(s);
    }
    private static boolean chargeDouble(String s){
        if (s.length() == 0)
            return false;
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '.'){
                count++;
            }
        }
        if (count > 1)
            return false;
        if (s.charAt(0) == '+' || s.charAt(0) == '-'){
            s = s.substring(1);
        }
        String[] son = s.split("\\.");
        if (son.length == 2){
            if (son[1].length() == 0){
                return chargeInt(son[0]);
            }
            if (son[0].length() == 0){
                if (son[1].charAt(0) == '+' || son[1].charAt(0) == '-'){
                    return false;
                }
                return chargeInt(son[1]);
            }
            return chargeInt(son[0]) && chargeInt(son[1]);
        }else if (son.length == 1){
            return chargeInt(son[0]);
        }
        return false;
    }
    private static boolean chargeInt(String s){
        if (s.length() == 0)
            return false;
        int index = 0;
        if (s.charAt(0) == '+' || s.charAt(0) == '-'){
            index++;
        }
        if (index >= s.length()){
            return false;
        }
        while (index < s.length()){
            if (s.charAt(index) < '0' || s.charAt(index) > '9'){
                return false;
            }
            index++;
        }
        return true;
    }
}
